package com.bolingcavalry.crm.phone_record.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.phone_record.service.PhoneRecordService;
import com.bolingcavalry.crm.security.utils.SecurityUtils;
import com.bolingcavalry.crm.utils.AUtils;
import com.entity.phone_records.PhoneRecord;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 电话记录表 前端控制器
 */
@Api(tags = "电话记录管理")
@RestController
@RequestMapping("/phone-record")
public class PhoneRecordController {

    @Autowired
    private PhoneRecordService phoneRecordService;
    @Autowired
    private AUtils aUtils;

    /**
     * 新增电话记录
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody PhoneRecord phoneRecord) {
        if(phoneRecord.getCustomerCompany().isEmpty()){
            return ResponseResult.error("客户公司不能为空");
        }
        phoneRecord.setCreatedTime(LocalDateTime.now());
        phoneRecord.setUpdatedTime(LocalDateTime.now());
        phoneRecord.setCreateBy(aUtils.getNickNameByuserName(SecurityUtils.getCurrentUsername()));
        boolean success = phoneRecordService.save(phoneRecord);
        if (success) {
            return ResponseResult.success("新增成功", phoneRecord);
        } else {
            return ResponseResult.error("新增失败");
        }
    }

    /**
     * 修改电话记录
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody PhoneRecord phoneRecord) {
        if (phoneRecord.getId() == null) {
            return ResponseResult.error("ID不能为空");
        }
        phoneRecord.setUpdatedTime(LocalDateTime.now());
        boolean success = phoneRecordService.updateById(phoneRecord);
        if (success) {
            return ResponseResult.success("修改成功", phoneRecord);
        } else {
            return ResponseResult.error("修改失败或记录不存在");
        }
    }

    /**
     * 分页查询电话记录
     */
    @GetMapping("/list")
    public ResponseResult<PhoneRecord> list(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "createBy", required = false) String createBy,
            @RequestParam(value = "customerCompany", required = false) String customerCompany,
            @RequestParam(value = "status", required = false) String status) { // 查询条件：创建人姓名

        IPage<PhoneRecord> page = phoneRecordService.findPhoneRecords(pageNum, pageSize, createBy,customerCompany,status);

        // 封装分页结果
        PageResult<PhoneRecord> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());

        return ResponseResult.success("查询成功", result);
    }

    /**
     * 根据ID查询电话记录
     */
    @GetMapping("/get/{id}")
    public ResponseResult<PhoneRecord> get(@PathVariable Long id) {
        PhoneRecord record = phoneRecordService.getById(id);
        if (record != null) {
            return ResponseResult.success("查询成功", record);
        } else {
            return ResponseResult.error("记录不存在");
        }
    }

    /**
     * 根据ID删除电话记录
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        boolean success = phoneRecordService.removeById(id);
        if (success) {
            return ResponseResult.success("删除成功", null);
        } else {
            return ResponseResult.error("删除失败或记录不存在");
        }
    }
}